package com.gviiii.baiduai.util;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * json工具类
 * @author zzh
 */
public class JsonUtil {

    @SuppressWarnings("deprecation")
    private static ObjectMapper objectMapper = new ObjectMapper();
    static {
        objectMapper
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) // 禁止使用出现未知属性之时，抛出异常
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
                .setSerializationInclusion(Include.NON_EMPTY)
                .setFilterProvider(new SimpleFilterProvider().setFailOnUnknownId(false))
                .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
                .setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    }
    /**
     * 将对象转换成json字符串
     *
     * @param <T>
     *
     * @param t
     * @return
     */
    public static <T> String witeStringByObject(T t) {
        try {
            return objectMapper.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 从字符串中读取对象
     *
     * @param str
     * @param clazz
     * @return
     */
    public static <T> Object readObjectByString(String str, Class<T> clazz) {
        try {
            return objectMapper.readValue(str, clazz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return clazz;
    }

    /**
     * @param str
     * @param collectionType
     * @param <E>
     * @return
     */
    public static <E> E readObjectByString(String str, JavaType collectionType) {
        try {
            return objectMapper.readValue(str, collectionType);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param str
     * @param typeReference
     * @param <E>
     * @param <T>
     * @return
     */
    public static <E, T> E readObjectByString(String str,TypeReference<T> typeReference) {
        try {
            return objectMapper.readValue(str, typeReference);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取泛型的Collection Type
     *
     * @param collectionClass
     *            泛型的Collection
     * @param elementClasses
     *            元素类
     * @return JavaType Java类型
     * @since 1.0
     */
    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }

    /**
     * 将map转化为json字符串
     *
     * @param jsonmap
     * @return
     */
    public static String writeJsonStringByMap(Map<String, Object> jsonmap) {
        return JsonUtil.witeStringByObject(jsonmap);
    }
}

